package rexsee.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.InputType;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SingleLinePromptDialog extends AlertDialog {

	public static abstract class SingleLinePromptDialogCallback {
		public abstract void run(String input);
	}

	public SingleLinePromptDialog(Context context, int icon, int title, int message, String defaultValue, final SingleLinePromptDialogCallback callback) {
		this(context, context.getResources().getDrawable(icon), context.getResources().getString(title), context.getResources().getString(message), defaultValue, InputType.TYPE_CLASS_TEXT, callback);
	}
	public SingleLinePromptDialog(Context context, Drawable icon, String title, String message, String defaultValue, final SingleLinePromptDialogCallback callback) {
		this(context, icon, title, message, defaultValue, InputType.TYPE_CLASS_TEXT, callback);
	}
	public SingleLinePromptDialog(Context context, int icon, int title, int message, String defaultValue, int inputType, final SingleLinePromptDialogCallback callback) {
		this(context, context.getResources().getDrawable(icon), context.getResources().getString(title), context.getResources().getString(message), defaultValue, inputType, callback);
	}
	public SingleLinePromptDialog(Context context, Drawable icon, String title, String message, String defaultValue, int inputType, final SingleLinePromptDialogCallback callback) {

		super(context, android.R.style.Theme_Panel);

		setIcon(icon);
		setTitle(Html.fromHtml(title));
		setCancelable(false);

		TextView text = new TextView(context);
		text.setPadding(0, 0, 0, 10);
		text.setTextSize(20);
		text.setTextColor(Color.WHITE);
		text.setText(Html.fromHtml(message));

		EditText editBox = new EditText(context);
		editBox.setMinHeight(24);
		editBox.setId(0);
		editBox.setSelectAllOnFocus(true);
		editBox.setSingleLine(false);
		editBox.setText(defaultValue);
		editBox.setInputType(inputType);

		final LinearLayout layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setPadding(10, 10, 10, 10);
		layout.addView(text, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		layout.addView(editBox, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		setView(layout);

		setButton(DialogInterface.BUTTON_NEGATIVE, context.getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int whichButton) {
				dismiss();
				callback.run(((TextView) layout.findViewById(0)).getText().toString());
			}
		});
		setButton(DialogInterface.BUTTON_POSITIVE, context.getResources().getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int whichButton) {
				dismiss();
			}
		});
		show();

		editBox.requestFocus();
		InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInput(editBox, 0);

	}

}
